Skip to content

fix(push): surface account lookup errors instead of reporting missing handle - #5950

Open
RainyMarks wants to merge 1 commit into
tscircuit:mainfrom
RainyMarks:fix/5275-distinguish-fetch-account-errors
Open

RainyMarks wants to merge 1 commit into
tscircuit:mainfrom
RainyMarks:fix/5275-distinguish-fetch-account-errors

Conversation

@RainyMarks

Copy link
Copy Markdown

Summary

Closes #5275

Fixes tsci push misreporting "You need to set your tscircuit handle to publish packages" when looking up the authenticated account for TSCIRCUIT_TOKEN fails due to HTTP errors (such as 503, 401 session_expired, 403, 429), network/timeout errors, or malformed responses.

Root Cause

  • fetchAccount() in lib/registry-api/fetch-account.ts caught every request and JSON-parsing error and returned null, and did not validate that the accounts/get response contained an account object.
  • pushSnippet() in lib/shared/push-snippet.ts treated any falsy (await fetchAccount())?.tscircuit_handle as proof that the account lacked a handle, discarding the underlying registry/network error and getRegistryApiKy()'s expired-session guidance.
  • The same swallowed-error behavior caused tsci auth whoami to print @(not set) with exit code 0 on lookup failure, and tsci auth login to print Already logged in as @undefined! when no saved handle was present.

Changes

  • lib/registry-api/fetch-account.ts:
    • Add optional { sessionToken?: string; throwOnError?: boolean } parameter to fetchAccount() so existing offline-tolerant callers (such as tsci init and tsci auth login) keep their default null fallback while strict callers can surface errors.
    • Validate that the accounts/get payload contains a non-null account object (throwing a descriptive error when missing or when an error payload is returned).
  • lib/registry-api/endpoint-types.ts:
    • Update EndpointResponse["accounts/get"]["account"]["tscircuit_handle"] to string | null to match accounts without a handle set.
  • lib/shared/push-snippet.ts:
    • Call fetchAccount({ sessionToken, throwOnError: true }) when TSCIRCUIT_TOKEN is set, reporting Could not verify your account for publishing: <error> with exit code 1 on lookup failure without falling back to a cached tscircuitHandle.
    • Keep "You need to set your tscircuit handle to publish packages" strictly for accounts that were successfully retrieved without a handle (or saved sessions without a handle).
  • cli/auth/whoami/register.ts:
    • Call fetchAccount({ sessionToken, throwOnError: true }), printing Failed to fetch account details: <error> to stderr and setting process.exitCode = 1 on lookup failures instead of printing @(not set) with exit code 0.
    • Avoid falling back to cached accountId/sessionId when TSCIRCUIT_TOKEN is active.
  • cli/auth/login/register.ts:
    • Keep offline tolerance when already authenticated while avoiding @undefined and avoiding stale cached handles when TSCIRCUIT_TOKEN is set.
  • Offline unit tests:
    • Added tests/lib/registry-api/fetch-account.test.ts covering default offline fallback, throwOnError on HTTP 503/401 (session_expired)/403/429, network errors, malformed JSON, missing account objects, accounts with/without tscircuit_handle, and skip/unauthenticated paths.
    • Added tests/lib/push-snippet-account-lookup.test.ts covering pushSnippet (503 with cached handle present, 401 session_expired, network error, malformed payload, 200 without handle, 200 with handle proceeding to publish), auth whoami (503 failure vs 200 without handle), and auth login (offline-tolerant 503 without @undefined).

Verification

  • bun test tests/lib/registry-api/fetch-account.test.ts tests/lib/push-snippet-account-lookup.test.ts tests/lib/cli-config-token.test.ts tests/lib/registry-api/get-ky-session-expired.test.ts tests/lib/registry-api/get-ky-error-output.test.ts — 28 pass, 0 fail.
  • bun test --timeout 30000 tests/cli/auth/whoami.test.ts tests/cli/auth/login.test.ts tests/cli/push/push15-environment-token.test.ts — 4 pass, 0 fail.
  • bunx tsc --noEmit — passed (exit 0).
  • bunx biome format on all 7 modified/added files and bunx biome check on core/new files — passed (exit 0).
  • bun run build — passed (exit 0).
  • Verified end-to-end CLI reproduction with a mocked HTTP 503 /accounts/get response:
    • tsci push index.circuit.tsx exits with code 1 and reports Could not verify your account for publishing: FAIL [503]: POST /accounts/get.
    • tsci auth whoami exits with code 1 and reports Failed to fetch account details: FAIL [503]: POST /accounts/get.
    • tsci auth login exits with code 0 and reports Already logged in! Use tsci logout to switch accounts..

… handle

Distinguish registry/network account lookup failures from a successfully
retrieved account that lacks a tscircuit_handle:

- Add optional throwOnError and sessionToken options to fetchAccount() and
  validate that the accounts/get response contains an account object, while
  preserving the default null fallback for offline-tolerant callers.
- Update pushSnippet() to verify TSCIRCUIT_TOKEN accounts with throwOnError
  so HTTP errors (401/403/429/503), network failures, and malformed payloads
  report an account verification failure instead of a missing handle.
- Update auth whoami to report account lookup failures with a nonzero exit
  code instead of printing @(not set) and exiting 0.
- Update auth login to stay offline-tolerant while avoiding @undefined and
  stale cached handles when TSCIRCUIT_TOKEN is set.
- Add offline unit tests for fetchAccount, pushSnippet, auth whoami, and
  auth login.

Closes tscircuit#5275
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

push reports a missing handle when TSCIRCUIT_TOKEN account lookup fails

1 participant